<template>
  <a-modal
    title="客户资料详情"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    :footer="null"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="客户资料名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback>
              <a-input
                readOnly
                placeholder="请输入客户资料名称"
                v-decorator="['cusInforName', {rules: [{required: true, message: '请输入客户资料名称！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="社会统一信用代码"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback>
              <a-input
                readOnly
                placeholder="请输入社会统一信用代码"
                v-decorator="['code', {rules: [{required: true, message: '请输入社会统一信用代码！'}]}]" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="客户分类"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback>
              <a-tree-select
                :open="false"
                v-decorator="['cusSortId', {rules: [{ required: true, message: '请选择客户分类！' }]}]"
                style="width: 100%"
                :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
                :treeData="cusTree"
                placeholder="请选择客户分类">
                <span slot="title" slot-scope="{ id }">{{ id }}</span>
              </a-tree-select>
            </a-form-item>
          </a-col>
        </a-row>

        <cus-person-table-form-item
          ref="cusPersonTable"
          :detailReadOnly="readOnly"
          :oldTableData="cusPersonList"
          @tableDataChange="(data) => {cusPersonList = data}"
          @add="({tableData})=>{ cusPersonList = tableData}"
          @del="({tableData})=>{ cusPersonList = tableData}"
        />
        <a-form-item
          label="备注信息"
          :labelCol="labelCol_T"
          :wrapperCol="wrapperCol_T"
          has-feedback>
          <a-textarea
            readOnly
            placeholder="请输入备注信息"
            v-decorator="['remark']"
            :auto-size="{ minRows: 3, maxRows: 6 }"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { getCusTree } from '@/api/modular/main/cussort/cusSortManage'
  import cusPersonTableFormItem from '@/views/main/cusinfor/cusPersonTableFormItem'
  import { cusPersonList } from '@/api/modular/main/cusperson/cusPersonManage'
  export default {
    components: {
      cusPersonTableFormItem
    },
    data() {
      return {
        cusPersonList: [],
        // 联系人list
        allCusPersonList: [],
        // 控制查看页面的vxe-table只读
        readOnly: true,
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        labelCol_T: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol_T: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        cusTree: [],
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        cusSortIdData: []
      }
    },
    created() {
      this.getAllCusPersonList()
    },
    methods: {
      // 获取联系人list
      async getAllCusPersonList() {
        this.allCusPersonList = []
        var promise = cusPersonList().then(res => {
          this.allCusPersonList = res.data
        })
        await promise
        return this.allCusPersonList
      },
      /**
       * 获取类型树，并加载于表单中
       */
      getCusTree() {
        getCusTree().then((res) => {
          this.formLoading = false
          if (!res.success) {
            this.cusTree = []
            return
          }
          this.cusTree = res.data
        })
      },
      // 初始化方法
      async detail (record) {
        this.visible = true
        const tempConPersonList = []
        // 获取客户资料id对应的联系人list
        const data = await this.getAllCusPersonList()
        data.forEach(item => {
          if (item.cusInforId === record.id) {
            tempConPersonList.push(item)
          }
        })
        this.cusPersonList = [...tempConPersonList]
        this.getCusTree()
        this.$nextTick(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              cusInforName: record.cusInforName,
              code: record.code,
              cusSortId: record.cusSortId,
              remark: record.remark
            }
          )
        })
      },
      handleCancel() {
        this.cusPersonList = []
        this.form.resetFields()
        this.visible = false
      }
    },
    watch: {
      // 监听cusPersonList变化
      cusPersonList: {
        handler() {
          this.$nextTick(() => {
            this.$refs.cusPersonTable.refreshTable(this.cusPersonList)
          })
        }
      }
    }
  }
</script>
